package com.ruoyi.miniapp.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.miniapp.api.domain.AppUser;
import com.ruoyi.common.core.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.miniapp.service.IAppUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户登录Controller
 * 
 * @author ruoyi
 * @date 2022-10-26
 */
@RestController
@RequestMapping("/appuser")
public class AppUserController extends BaseController
{
    @Autowired
    private IAppUserService appUserService;

    /**
     * 查询用户登录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(AppUser appUser)
    {
        startPage();
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        return getDataTable(list);
    }

    /**
     * 导出用户登录列表
     */
    @Log(title = "用户登录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppUser appUser)
    {
        List<AppUser> list = appUserService.selectAppUserList(appUser);
        ExcelUtil<AppUser> util = new ExcelUtil<AppUser>(AppUser.class);
        util.exportExcel(response, list, "用户登录数据");
    }

    /**
     * 获取用户登录详细信息
     */
    @GetMapping(value = "/{loginId}")
    public AjaxResult getInfo(@PathVariable("loginId") Long loginId)
    {
        return AjaxResult.success(appUserService.selectAppUserByLoginId(loginId));
    }

    /**
     * 新增用户登录
     */
    @Log(title = "用户登录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppUser appUser)
    {
        return toAjax(appUserService.insertAppUser(appUser));
    }

    /**
     * 修改用户登录
     */
    @Log(title = "用户登录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppUser appUser)
    {
        return toAjax(appUserService.updateAppUser(appUser));
    }

    /**
     * 删除用户登录
     */
    @Log(title = "用户登录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{loginIds}")
    public AjaxResult remove(@PathVariable Long[] loginIds)
    {
        return toAjax(appUserService.deleteAppUserByLoginIds(loginIds));
    }

    /**
     * 获取用户Tel获取详细信息
     */
    @InnerAuth
    @GetMapping(value = "/find/{tel}")
    public R<LoginUser<AppUser>> getAppUserInfoByTel(@PathVariable("tel") String tel)
    {
        AppUser appUser = appUserService.selectUserByTel(tel);
        LoginUser<AppUser> loginUser = new LoginUser<>();
        loginUser.setUser(appUser);
        return R.ok(loginUser);
    }
}
